/**
 * @file shutdown.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SHUTDOWN != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void shutdown_cancel_btn_action(lv_obj_t * btn, lv_event_t event);
static void shutdown_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/
#if USE_POWER_OFF_GIF
static lv_task_t * shutdown_task_p;
#endif
static uint8_t   shutdown_performance_flg = WATCH_SHUTDOWN_UNPERFORMED;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * shutdown_create(lv_obj_t * activity_obj, bool confirm)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SHUTDOWN;
        activity_ext.create = NULL;
        activity_ext.prepare_destory = shutdown_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    if(confirm) {
        lv_obj_t * label = lv_label_create(obj, NULL);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_obj_set_click(label, false);
        lv_label_set_text_id(label, WATCH_TEXT_ID_SHUTDOWN);
        lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

        label = lv_label_create(obj, NULL);
        lv_obj_set_width(label, lv_obj_get_width(obj) - 40);
        lv_label_set_text_id(label, WATCH_TEXT_ID_CONFIRM_SHUTDOWN);
        lv_obj_align(label, obj, LV_ALIGN_CENTER, 0, -40);

        lv_obj_t  * btn = lv_imgbtn_create(obj, NULL);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_CONFIRM_BIG);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_CONFIRM_BIG);
        lv_obj_set_event_cb(btn, shutdown_confirm_btn_action);
        lv_watch_obj_add_element(btn);
        lv_obj_align(btn, obj, LV_ALIGN_IN_BOTTOM_LEFT, 20, -20);

        btn = lv_imgbtn_create(obj, NULL);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_CANCEL_BIG);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_CANCEL_BIG);
        lv_obj_set_event_cb(btn, shutdown_cancel_btn_action);
        lv_watch_obj_add_element(btn);
        lv_obj_align(btn, obj, LV_ALIGN_IN_BOTTOM_RIGHT, -20, -20);
    } else {
        shutdown_confirm_btn_action(NULL, LV_EVENT_CLICKED);
    }

    return obj;
}

void shutdown_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;

    lv_obj_t * obj = shutdown_create(NULL, true);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void shutdown_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void shutdown_cancel_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    lv_watch_obj_cleanup(btn);
}

#if USE_POWER_OFF_GIF
static void shutdown_poweroff(void)
{
    lv_task_del(shutdown_task_p);
    shutdown_task_p = NULL;
    Hal_Power_Off(HAL_TYPE_POWER_OFF);
}

void shutdown_music(void)
{
    Hal_NFFS_File_Play_Onetime(AUDIO_POWER_OFF, query_current_volume(), NULL, 0);
    printf("shutdown_music.\n");
}

static void shutdown_task(lv_task_t * task)
{
    gif_info_t * info = (gif_info_t *)task->user_data;

    if(info->once_done) {
#if 0
        gif_close(info);
        memset((void *)info, 0, sizeof(gif_info_t));
        Hal_Backlight_Off();
#endif
        Hal_Tone_Play_End();
        shutdown_poweroff();
    }
}
#endif

uint8_t get_shutdown_performance_flg(void)
{
    return shutdown_performance_flg;
}

void shutdown_confirm_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    if(WATCH_SHUTDOWN_UNPERFORMED == shutdown_performance_flg) {
        shutdown_performance_flg = WATCH_SHUTDOWN_PERFORMING;
        printf("shutdown start\n");
    } else {
        printf("shutdown has been performed\n");
        return;
    }
    watch_set_ready_state(false);

    lv_task_del(lowbattery_task_p);
    lowbattery_task_p = NULL;
#if USE_LV_WATCH_SIMCHECK
    lv_task_del(checksim_task_p);
    checksim_task_p = NULL;
#endif

    if(NULL != btn) {
        lv_obj_clean(lv_obj_get_parent(btn));
    }
    lv_watch_go_home();
#if USE_LV_BLUETOOTH != 0
    bt_power_off();
#endif
    dial_poweroff();
    watch_set_suspend_enable(false, ACT_ID_SHUTDOWN, 0);
#if USE_POWER_OFF_GIF
    shutdown_music();

    lv_obj_t * activity_obj = NULL;
    lv_watch_activity_ext_t activity_ext;
    memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
    activity_ext.actId = ACT_ID_ANYOBJ;
    activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    LV_ASSERT_MEM(activity_obj);
    if(activity_obj == NULL) return;

    gif_info_t * gif_info = gif_open(activity_obj, ANIM_POWER_OFF, 1);
    if(!gif_info) {
        printf("shutdown gif failed.\n");
        return;
    }
    shutdown_task_p = lv_task_create(shutdown_task, 100, LV_TASK_PRIO_MID, (void *)gif_info);
#else
    printf("shutdown without power off gif.\n");
    Hal_Backlight_Off();
    lv_task_del(lowbattery_task_p);
    Hal_Power_Off(HAL_TYPE_POWER_OFF);
#endif
    return;
}

#endif /*USE_LV_WATCH_SHUTDOWN*/
